import org.apache.commons.beanutils.DynaBean;
import org.junit.Test;

import net.sf.json.JSONArray;
import net.sf.json.JSONObject;
import net.sf.json.JSONSerializer;
import net.sf.json.JsonConfig;

/**
 * http://json-lib.sourceforge.net/snippets.html
 * @author yang
 *
 */
public class JsonLib {
	/**
	 * 将java对象转换为json字符串
	 */
	@Test
	public void test1(){
		Cat cat = new Cat("blue",10);
		JSONObject jsonObject = JSONObject.fromObject(cat);
		System.out.println(jsonObject);
	}
	/**
	 * 将json字符串转为java对象
	 */
	@Test
	public void test2(){
		String content = "{\"color\":\"blue\",\"age\":10}";
		JSONObject jsonObject = JSONObject.fromObject(content);
		Cat cat = (Cat) JSONObject.toBean(jsonObject, Cat.class);
		System.out.println(cat);
	}
	/**
	 * 组装json字符串
	 */
	@Test
	public void test3(){
		JSONObject jsonObject = new JSONObject();
		jsonObject.element("aa", "bb");
		//put和element的区别：put会去判断jsonObject中是否已经含有该值
		
		Object  previous = jsonObject.put("aa", "cc");
		System.out.println(previous);
		
		previous = jsonObject.put("bb", "cc");
		System.out.println(previous);
		
		jsonObject.put("bb", "dd");
		//accumulate:若jsonObject中已经含有该值，则追加
		jsonObject.accumulate("aa", "dd");
		jsonObject.accumulate("cc", "ee");
		//opt:取值操作，如果jsonObject中不含该值，则显示默认值
		String temp = jsonObject.optString("aa", "ee");
		System.out.println(temp);
		System.out.println(jsonObject);
	}
	/**
	 * 使用JSONSerializer实现java和json的转变
	 */
	@Test
	public void test4(){
		String content = "{\"color\":\"blue\",\"age\":10}";
		JSONObject jsonObject = (JSONObject) JSONSerializer.toJSON(content);
		System.out.println(jsonObject);
		
		DynaBean bean = (DynaBean) JSONSerializer.toJava( jsonObject ); 
		System.out.println(bean.get("color"));
		
		JsonConfig config = new JsonConfig();
		config.setRootClass(Cat.class);
		Cat cat = (Cat) JSONSerializer.toJava(jsonObject, config);
		System.out.println(cat);
	}
	
	@Test
	public void test5(){
		JSONObject jsonObject = new JSONObject();
		//
		jsonObject.accumulate("type", "FeatureCollection");
		JSONObject crs = new JSONObject();
		crs.accumulate("type", "name");
		crs.accumulate("properties", new JSONObject().element("name", "EPSG:4326"));
		jsonObject.accumulate("crs",crs);
		//
		JSONObject point1 = new JSONObject();
		point1.accumulate("type", "Feature");
		point1.accumulate("geometry", new JSONObject().element("type", "Point").accumulate("coordinates",new double[]{117.39454,39.14294}));
		point1.accumulate("properties", new JSONObject().element("type", "Point2"));
		//
		JSONObject point2 = new JSONObject();
		point2.accumulate("type", "Feature");
		point2.accumulate("geometry", new JSONObject().element("type", "Point").accumulate("coordinates",new double[]{117.39976,39.13532}));
		point2.accumulate("properties", new JSONObject().element("type", "Point2"));
		//
		JSONObject lineString1 = new JSONObject();
		lineString1.accumulate("type", "Feature");
		double[][] coord1 = new double[][]{new double[]{117.39454, 39.14294},new double[]{117.39668, 39.13954},new double[]{117.40049,39.13981}};
		JSONArray array = new JSONArray();
		array.add(new double[]{117.39454, 39.14294});
		array.add(new double[]{117.39668, 39.13954});
		array.add(new double[]{117.40049,39.13981});
		lineString1.accumulate("geometry", new JSONObject().element("type", "LineString").accumulate("coordinates",array));
		lineString1.accumulate("properties", new JSONObject().element("type", "LineString1"));
		//
		JSONObject lineString2 = new JSONObject();
		lineString2.accumulate("type", "Feature");
		double[][] coord2 = new double[][]{new double[]{117.39976,39.13532},new double[]{117.40476,39.14260},new double[]{117.40049,39.13981}}; 
		lineString2.accumulate("geometry", new JSONObject().element("type", "LineString").accumulate("coordinates",coord2));
		lineString2.accumulate("properties", new JSONObject().element("type", "LineString2"));
		//
		jsonObject.accumulate("features", point1);
		jsonObject.accumulate("features", point2);
		jsonObject.accumulate("features", lineString1);
		jsonObject.accumulate("features", lineString2);
		System.out.println(jsonObject.toString());
	}
	
	@Test
	public void test6(){
		
	}

}
